<template>
  <div>
    <div class="mb">
      <el-input
        placeholder="请输入姓名"
        v-model="name"
        style="width: 200px; margin-right: 10px"
      ></el-input>
      <el-button type="primary" @click="serch()">查询</el-button>
      <el-button icon="el-icon-plus" @click="add()" type="primary"></el-button>
    </div>
    <el-table :data="data" border>
      <el-table-column label="学号" prop="studentNo"></el-table-column>
      <el-table-column label="姓名" prop="name"></el-table-column>
      <el-table-column label="性别" prop="sex"></el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="success" @click="bj(scope.row)">编辑</el-button>

          <el-button type="dange" @click="sc(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[100, 200, 300, 400]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form :model="form">
        <el-form-item label="学号">
          <el-input v-model="form.studentNo" type="number"></el-input>
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-select v-model="form.sex" style="width: 100%">
            <el-option label="男" value="男"></el-option>
            <el-option label="女" value="女"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="qx()">取 消</el-button>
        <el-button type="primary" @click="submit()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import service from "@/request";

export default {
  data() {
    return {
      title: "新增",
      data: [],
      dialogVisible: false,
      form: {},
      name: "",
      total: 0,
      pageNum: 1,
      pageSize: 10,
      data1: [],
    };
  },
  mounted() {
    this.getdata();
    this.getdata1();
  },
  methods: {
    sc(id){
          this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
const res=await service.get("/student/delete",{params:{
    id:id
}})
if(res.code == 200){
      this.$message({
            type: 'success',
            message: '删除成功!'
          });
    this.getdata()
}
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
    add() {
      this.title = "新增";
      this.dialogVisible = true;
      this.form = {};
    },
    bj(row) {
      this.title = "编辑";
      this.form = row;
      this.dialogVisible = true;
    },
    submit() {
      this.dialogVisible = false;
    },
    handleClose() {
      this.dialogVisible = false;
    },
    qx() {
      this.form = {};
    },
    async submit() {
      let sfxs = false;
      this.data1.forEach((el) => {
        if (el.studentNo == this.form.studentNo) {
          sfxs = true;
        } else {
          sfxs = false;
        }
      });
      if (sfxs) {
        this.$message.warning("学号重复，请重新输入");
      } else {
        if (this.title == "新增") {
          this.form.username = this.form.studentNo;
          this.form.password = this.form.studentNo;
          const res = await service.post("/student/add", this.form);
          console.log(res);
          if (res.code == 200) {
            this.dialogVisible = false;
            this.$message.success(res.msg);
            this.getdata();
          }
        } else {
          this.form.username = this.form.studentNo;
          this.form.password = this.form.studentNo;
          const res = await service.post("/student/update", this.form);
          console.log(res);
          if (res.code == 200) {
            this.dialogVisible = false;
            this.$message.success(res.msg);
            this.getdata();
          }
        }
      }
    },
    handleCurrentChange(e) {
      this.pageNum = e;
      this.getdata();
    },
    handleSizeChange(e) {
      this.pageSize = e;
      this.getdata();
    },
    serch() {
      this.pageNum = 1;
      this.pageSize = 10;
      this.getdata();
    },
    async getdata() {
      const res = await service.get("/student/index", {
        params: {
          name: this.name,
          pageNum: this.pageNum,
          pageSize: this.pageSize,
        },
      });
      this.data = res.data;
      this.total = res.total;
    },
    async getdata1() {
      const res = await service.get("/student/all", {});
      this.data1 = res.data;
    },
  },
};
</script>
<style scoped>
.mb {
  margin-bottom: 10px;
}
</style>